package com.example.demo.controller;

import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/16.
 */
@RestController
@RequestMapping("/transaction")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/create")
    public void create(){
        userService.create("rose","21");
    }

    @RequestMapping("/transaction_test")
    public void transaction_test(){
        userService.transaction_test("jack","25");
    }

    /**
     * 事务流程入口
     * @return
     */
    @RequestMapping("/transaction_entrance")
    public String transaction_entrance(){
        Map<String,String> paramMap = new HashMap<String, String>();
        paramMap.put("name","fen bu shi transaction 1");
        paramMap.put("age","12");
        int rv = userService.transaction_user(paramMap);
        if (rv > 0) {
            System.out.println("=============》新增成功");
            return "新增成功";
        } else {
            System.out.println("=============》新增失败，但没回滚事务，检查是否表已存在数据");
            return "新增失败";
        }
    }
}
